package com.hz.ruoyiquality.controller;

import com.hz.rouyigonggong.domain.dto.LgOutOrderDTO;
import com.hz.ruoyiquality.api.service.QuelityService;
import com.hz.ruoyiquality.domain.dto.LgInboundOrderDTO;
import com.hz.ruoyiquality.domain.dto.OrderUpdateDTO;
import com.hz.ruoyiquality.service.OrderService;
import com.hz.ruoyiquality.service.QrCodeService;
import com.ruoyi.common.core.web.domain.AjaxResult;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private QuelityService quelityService;
    @Autowired
    private QrCodeService qrCodeService;


    @PostMapping("/updateOrder")
//    @GlobalTransactional//分布式事务
    public AjaxResult updateOrder(@RequestBody OrderUpdateDTO orderUpdateDTO) {
        //通过远程接口修改入库订单状态  2 为质检完成  0 1 2
        LgInboundOrderDTO lgInboundOrderDTO = new LgInboundOrderDTO();
        lgInboundOrderDTO.setOrderNumber(orderUpdateDTO.getOrderNo());
        lgInboundOrderDTO.setStatus(orderUpdateDTO.getStatus()+1);   //改为2

        quelityService.edit(lgInboundOrderDTO);

        //远程接口修改出库，质检订单状态
        LgOutOrderDTO lgOutOrderDTO = new LgOutOrderDTO();
        lgOutOrderDTO.setStockOrderNumber(orderUpdateDTO.getOrderNo());

        if (orderService.needVehicle(orderUpdateDTO.getOrderNo())!=null){
            //判断是否需要车辆   0需要待指派车辆   1不需要车辆，直接调转到质检完成
            //0需要待指派车辆
            System.out.println("**************判断是否需要车辆*******************"+orderService.needVehicle(orderUpdateDTO.getOrderNo()));
            if (orderService.needVehicle(orderUpdateDTO.getOrderNo())==0){
                lgOutOrderDTO.setOrderStatus(orderUpdateDTO.getStatus()+4);    //改为5
            }else if (orderService.needVehicle(orderUpdateDTO.getOrderNo())==1){
                lgOutOrderDTO.setOrderStatus(orderUpdateDTO.getStatus()+1); //改为2
            }
        }


        quelityService.editoutorder(lgOutOrderDTO);
        try {
            // 生成并上传二维码
            Integer orderNo = orderUpdateDTO.getOrderNo();  // 获取订单号
            String qrUrl = qrCodeService.generateAndUploadQr(orderNo);

            if (qrUrl != null) {
                // 存入数据库
                qrCodeService.saveToDatabase(orderNo, qrUrl);
            }
           orderService.updateOrderStatus(orderUpdateDTO);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            return AjaxResult.error("error");
        }
    }

    /**
     * 调用入库接口  改为3 ,入库中
     * @param orderUpdateDTO
     * @return
     */
    @PostMapping("/callEdit")
//    @GlobalTransactional//分布式事务
    public AjaxResult callEdit(@RequestBody OrderUpdateDTO orderUpdateDTO) {
        LgInboundOrderDTO lgInboundOrderDTO = new LgInboundOrderDTO();
        lgInboundOrderDTO.setOrderNumber(orderUpdateDTO.getOrderNo());
        lgInboundOrderDTO.setStatus(orderUpdateDTO.getStatus()+2);   //改为3 ,入库中
        return quelityService.edit(lgInboundOrderDTO);
    }

    /**
     * 发送入库完成请求  , 修改入库订单状态为4
     * @param orderUpdateDTO
     * @return
     */
    @PostMapping("/updateRuStorageOrder")
//    @GlobalTransactional//分布式事务
    public AjaxResult updateRuStorageOrder(@RequestBody OrderUpdateDTO orderUpdateDTO) {
        //通过远程接口修改入库订单状态
        LgInboundOrderDTO lgInboundOrderDTO = new LgInboundOrderDTO();
        lgInboundOrderDTO.setOrderNumber(orderUpdateDTO.getOrderNo());
        lgInboundOrderDTO.setStatus(orderUpdateDTO.getStatus()+3);   //改为4   入库完成
        lgInboundOrderDTO.setVerifiedWeight(orderUpdateDTO.getWeight());        //重量
        lgInboundOrderDTO.setActualQuantity(orderUpdateDTO.getQuantity());   //数量
        lgInboundOrderDTO.setInboundDate(Timestamp.valueOf(LocalDate.now().atStartOfDay()));

        LgInboundOrderDTO info = quelityService.getInfo(orderUpdateDTO.getOrderNo());
          // 获取存储时间天数
        int storageDays = Integer.parseInt(info.getStorageDays());
            // 计算截止日期
        LocalDate expiryDate = LocalDate.now().plusDays(storageDays);
        // 转换为 Timestamp
        Timestamp expiryTimestamp = Timestamp.valueOf(expiryDate.atStartOfDay());
        // 设置截止日期
        lgInboundOrderDTO.setExpiryDate(expiryTimestamp);


        //还需要修改  我的质检表  入库和出库  订单状态
        orderService.updateRuStorageOrder(orderUpdateDTO);

        return quelityService.edit(lgInboundOrderDTO);
    }


}
